package com.example.user.customviewdemo.test;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by user on 2016/4/28.
 */
public class MyProgressView extends View {

    private Paint paint = new Paint();
    Path path = new Path();
    private float sweepAngle = 360;
    private float radiu = 100;
    private int color;
    private AnimatorSet set;

    public MyProgressView(Context context) {
        super(context);
    }

    public MyProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(20);

        startAnima();
    }



    public MyProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();

        float left = getWidth() / 2 - radiu;
        float top = getHeight() / 2 - radiu;

        float right = getWidth() / 2 + radiu;
        float bottom = getHeight() / 2 + radiu;

        paint.setColor(Color.argb(100,100,100,0));


        RectF rectF = new RectF(left, top, right, bottom);
        path.addArc(rectF, 0, sweepAngle);

        canvas.drawPath(path, paint);
    }

    public void startAnima() {
        final ValueAnimator arcAnimator = ValueAnimator.ofFloat(0, 300);
        arcAnimator.setDuration(3000);
        arcAnimator.setRepeatCount(ValueAnimator.INFINITE);
        arcAnimator.setRepeatMode(ValueAnimator.RESTART);
        arcAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweepAngle = (float) arcAnimator.getAnimatedValue();
                postInvalidate();
            }
        });

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0, 360);
        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
        objectAnimator.setDuration(3000);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            final ValueAnimator colorAnimator = ValueAnimator.ofArgb(Color.RED, Color.GREEN, Color.BLUE);
            colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
            colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    color = (int) colorAnimator.getAnimatedValue();
                    invalidate();
                }
            });
        }

        set = new AnimatorSet();
        set.playTogether(arcAnimator, objectAnimator);
        set.setInterpolator(new LinearInterpolator());
        set.start();

    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if(visibility == View.GONE || visibility == View.INVISIBLE){
            set.cancel();
        }

    }

}
